package cn.bonoon.services;

import java.util.ArrayList;
import java.util.List;

import org.batatasframework.rawdata.RawdataDefinition;
import org.batatasframework.rawdata.RawdataHandler;
import org.batatasframework.rawdata.RawdataImportResult;
import org.batatasframework.rawdata.RawdataImportedResult;
import org.batatasframework.rawdata.RawdataParser;
import org.batatasframework.web.RequestParameterReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import cn.bonoon.core.RawdataItem;
import cn.bonoon.core.RawdataStreamService;
import cn.bonoon.entities.PlaceLevel;
import cn.bonoon.entities.RawdataExportEntity;
import cn.bonoon.entities.RawdataImportEntity;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.support.services.ServiceSupport;

@Service
@Transactional(readOnly = true)
public class RawdataStreamServiceImpl extends ServiceSupport implements RawdataStreamService {
	private @Autowired RawdataHandler handler;

	@Override
	@Transactional
	public RawdataImportResult executeUpload(IOperator user, String catalog, RequestParameterReader parameter) throws Exception {
		return handler.executeUpload(catalog, entityManager, user, parameter);
	}

	@Override
	public List<RawdataItem> myItems(IOperator user, String catalog) {
		List<RawdataItem> items = new ArrayList<>();
		for(RawdataDefinition def : handler.getDefinitions(catalog)){
			if(def.visitable(entityManager, user)){
				items.add(def.toItem());
			}
		}
		return items;
	}

	@Override
	public RawdataImportedResult importResult(IOperator user, String catalog, RawdataImportResult data) {
		RawdataImportEntity rawdataImport = null;
		RawdataExportEntity rawdataExport = null;
		if(null != data.getEid()){
			
			rawdataExport = entityManager.find(RawdataExportEntity.class, data.getEid());
		}
		
		if(null == rawdataExport){
			
			rawdataImport = entityManager.find(RawdataImportEntity.class, data.getIid());
		}else{
			rawdataImport = rawdataExport.getSubject();
			Assert.isTrue(data.getIid().equals(rawdataImport.getId()), "数据出现异常！");
		}
		
		String key = rawdataImport.getKey();
		RawdataDefinition definition = handler.get(catalog, key);
		RawdataParser parser = definition.getParser();
		List<Object> items = parser.exportItems(entityManager, rawdataExport);
		RawdataImportedResult reset=new RawdataImportedResult();
		reset.setItems(items);
		reset.setRawdataExport(rawdataExport);
		reset.setVmpath(definition.getVmpath());
		return reset;
	}
	
	@Override
	public RawdataDefinition getDefinition(IOperator user, String catalog, String key) {
		return handler.get(catalog, key);
	}
	
	@Override
	public void export(IOperator user, RawdataExportEntity entity) {
		RawdataImportEntity subject = entity.getSubject();
		handler.get(subject.getCatalog(), subject.getKey()).getParser().exportItems(entityManager, entity);
	}
	
	@Override
	public RawdataImportedResult getDoubtBy(IOperator user, String catalog, String key,Long id,String[] ids) {
		RawdataDefinition definition = handler.get(catalog, key);
		RawdataParser parser = definition.getParser();
		return parser.parseDoubt(user, entityManager,id, ids);
	}

	@Override
	public List<RawdataItem> chooseItems(List<RawdataItem> items,PlaceLevel level,String key,Long ownerId) {
		
		List<RawdataItem> newItems=new ArrayList<>();
		if (!items.isEmpty()&&items.size()>0) {	
			for (int i = 0; i < items.size(); i++) {
				RawdataItem item=items.get(i);
				if (item.getKey().startsWith(key)) {
//					if (item.getKey().equals("RD010")||item.getKey().equals("RD011")) {
//						if (ownerId==441424000000L||ownerId==441223000000L) {
//							newItems.add(item);
//						}
//						newItems.add(item);
//					}else {
//						newItems.add(item);
//					}
					newItems.add(item);
				}
			}				
		}	
		return newItems;
	}

	@Override
	@Transactional
	public RawdataImportResult executeUpload2(IOperator user, String catalog, RequestParameterReader parameter,Long projectId) throws Exception {
		return handler.executeUpload2(catalog, entityManager, user, parameter,projectId);
	}
}
